/**
 * 
 */
package com.yidu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.domain.vo.UserVo;
import com.yidu.service.UserService;
import com.yidu.utils.EmailTools;
import com.yidu.utils.Pages;
import com.yidu.utils.State;
import com.yidu.utils.Tools;

/**用户控制器层
 * @author Administrator
 *	田家龙
 */
@Controller
@RequestMapping("User")
public class UserController {
	@Resource
	private UserService userService ;
	@RequestMapping("findAll")
	@ResponseBody
	public Map<String, Object> findAll(UserVo userVo , Pages pages , String page){
		pages.setCurPage(Integer.valueOf(page));
		List<UserVo> list = userService.findAll(userVo, pages);
		Map<String, Object> map = new HashMap<String, Object>();
		pages.setTotalRows(userService.count(userVo).intValue());
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", pages.getTotalRows());
		map.put("data", list);
		return map;
	}
	
	//注册发送验证码
	@RequestMapping("yzm")
	public void user(String email,HttpSession session) {
		String randomStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		Random rd = new Random();
		String str = "";
		for(int i=0;i<4;i++) {
			int t = rd.nextInt(randomStr.length());
			char c = randomStr.charAt(t);
			str+=c;
		}
		String yzm = str;
		session.setAttribute("yzm", yzm);
		
		//调用工具类发送短信
		EmailTools.sendEmail(email, "验证码", "您本次注册的验证码是："+yzm);
	}
	//注册验证验证码
	@ResponseBody
	@RequestMapping("yzmAdd")
	public State yzmAdd(String yzm,HttpSession session) {
		State bm=new State();
		String yzms= (String) session.getAttribute("yzm");
		if(yzms.equals(yzm)) {
			bm.setState(1);
		}else {
			bm.setState(0);
		}
		return bm;
	}
	/**
	 * 增加数据
	 * @param uservo
	 * @return
	 */
	@RequestMapping("insert")
	@ResponseBody
	public State insert(@RequestParam(value = "userImgs", required = false) MultipartFile userImgs ,UserVo uservo,HttpSession session) {
		String imge = Tools.springImg(userImgs, session);
		
		uservo.setUserImg(imge);
		if(uservo.getUserId()!=null && !"".equals(uservo.getUserId())) {
			userService.update(uservo);
		}else {
			userService.add(uservo);
		}
		State state = new State();
		state.setState(1);
		state.setMsg("操作成功");
		return state ;
	}
	/**
	 * 修改数据
	 */
	@RequestMapping("update")
	@ResponseBody
	public State update(UserVo uservo) {
		userService.update(uservo);
		State state = new State();
		state.setState(1);
		state.setMsg("操作成功");
		return state ;
	}
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping("findId")
	@ResponseBody
	public UserVo findId(String id) {
		UserVo userVo = userService.findById(id);
		return userVo;
	}
	/**
	 * 删除数据
	 * @param uservo
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public State delete(UserVo uservo) {
		userService.delete(uservo);
		State state = new State();
		state.setState(1);
		state.setMsg("操作成功");
		return state;
	}
	/**
	 * 查询账号密码
	 * @return
	 */
	@RequestMapping("findUser")
	@ResponseBody
	public UserVo findUser(UserVo userVo) {
		return userService.findAllUser(userVo);
	}
}
